﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Web;
using System.Web.Http;
using Sharp.Infrastructure;
using Sharp.Portal.Configuration;
using Sharp.Portal.Mvc.List;
using Sharp.Portal.Owin;

namespace Sharp.WebBooter.Controllers
{
    public class ConfigController : ApiController
    {
        [HttpGet]
        public List<KeyValue<string, string>> DataConfig()
        {
            var list = DataConfigManage.GetData();
            return list;
        }

        [HttpPost]
        public void PostDataConfig([FromBody] string data)
        {
            var list = SerializableHelper.DeSerialize<List<KeyValue<string, string>>>(data, DataFormart.Json);
            DataConfigManage.Save(list);
        }

        /// <summary>
        /// 保存自定义列信息
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public Result SaveCustomColumn(CustomColumnInfo data)
        {
            Result result = new Result();
            try
            {
                HttpContext current = HttpContext.Current;

                //if (data.Root != null)
                //    data.Moduleid = DataListBase.ParseModuleId(data.Root) + "_" + data.Moduleid;

                var key = "CustomColumns_" + data.Moduleid + "_" + HttpContext.Current.User.Identity.GetUserId();
                if (!CustomColumns.DicCustomColumns.TryGetValue(key, out var config))
                {
                    return result;
                }

                int index = config.IndexOf(data.Column);
                if (index == -1)
                {
                    result.Err = "找不到列，可能配置文件有问题。";
                    return result;
                }

                var column = config.Columns[index];
                switch (data.Cmd)
                {
                    case "Show":
                        column.Hidden = false;
                        result.Message = "已设置列显示。";
                        break;
                    case "Hide":
                        column.Hidden = true;
                        result.Message = "已设置列隐藏。";
                        break;
                    case "Resize":
                        column.Width = data.Width;
                        result.Message = "已重设列宽。";
                        break;
                    case "Move":
 
                        if (data.Index >= config.Columns.Count)//意味着拖到最后
                        {
                            config.Columns.RemoveAt(index);
                            config.Columns.Add(column);
                        }
                        else
                        {
                            config.Columns.RemoveAt(index);
                            config.Columns.Insert(data.Index, column);
                        }
                        result.Message = "已修改列排序。";
                        break;
                    case "Sort":
                        config.SortByColumn = column.Name;
                        config.Sorting = current.Request.Form["sort"] == "DESC";

                        result.Message = "排序字段已修改。";
                        break;
                }
                config.SaveToFile(data.Moduleid);

                return result;
            }
            catch (Exception e)
            {
                result.Err = e.Message;
                return result;
            }

        }

        public class CustomColumnInfo
        {
            [DataMember(Name = "cmd")]
            public string Cmd { get; set; }
            [DataMember(Name = "moduleid")]
            public string Moduleid { get; set; }
            [DataMember(Name = "Root")]
            public string Root { get; set; }
            [DataMember(Name = "column")]
            public string Column { get; set; }

            [DataMember(Name = "sort")]
            public string Sort { get; set; }
            [DataMember(Name = "width")]
            public int Width { get; set; }
            [DataMember(Name = "index")]
            public int Index { get; set; }
        }
    }
}